package com.jeasonzhao.commons.parser.lex;

public class LexTokenCollection extends java.util.Vector<LexToken>
{
    private static final long serialVersionUID = 1L;
    public LexTokenCollection()
    {
        super();
    }

    public LexToken[] toArray()
    {
        LexToken[] ret = new LexToken[this.size()];
        this.toArray(ret);
        return ret;
    }

    public LexTokenCollection slice(int nIndex,int nToIndex)
    {
        if(nToIndex < 0)
        {
            nToIndex = size();
        }
        return LexTokenCollection.from(subList(nIndex,nToIndex));
    }

    public static LexTokenCollection fromArray(LexToken[] arys)
    {
        return from(arys);
    }

    public static LexTokenCollection from(LexToken ...arys)
    {
        if(null == arys)
        {
            return null;
        }
        LexTokenCollection ret = new LexTokenCollection();
        for(int n = 0;n < arys.length;n++)
        {
            ret.addElement((LexToken) arys[n]);
        }
        return ret;
    }

    public static LexTokenCollection from(java.lang.Iterable<LexToken> coll)
    {
        if(null == coll)
        {
            return null;
        }
        LexTokenCollection ret = new LexTokenCollection();
        java.util.Iterator<LexToken> it = coll.iterator();
        while(it.hasNext())
        {
            ret.addElement(it.next());
        }
        return ret;
    }

    public String toString()
    {
        StringBuffer strRet = new StringBuffer();
        for(int n = 0;n < this.size();n++)
        {
            strRet.append(this.get(n).getInitString());
        }
        return strRet.toString();
    }

    public int find(String str,boolean isCaseSenstive)
    {
        if(null == str || str.length() < 1)
        {
            return -1;
        }
        for(int n = 0;n < this.size();n++)
        {
            if(null == this.elementAt(n))
            {
                continue;
            }
            String p = this.get(n).getToken();
            if(isCaseSenstive ? p.equals(str) : p.equalsIgnoreCase(str))
            {
                return n;
            }
        }
        return -1;
    }
    public int find(String str,LexTokenTypes t,boolean isCaseSenstive)
    {
        if(null == str || str.length() < 1)
        {
            return -1;
        }
        for(int n = 0;n < this.size();n++)
        {
            if(null == this.elementAt(n))
            {
                continue;
            }
            String p = this.get(n).getToken();
            if(t==this.get(n).getType() &&
               (isCaseSenstive ? p.equals(str) : p.equalsIgnoreCase(str)))
            {
                return n;
            }
        }
        return -1;
    }

    public String toDebugString()
    {
        StringBuffer strRet = new StringBuffer();
        strRet.append("\r\n");
        strRet.append("-------Lex Token Count = " + this.size() + "----------");
        for(int n = 0;n < this.size();n++)
        {
            LexToken t = this.get(n);
            strRet.append("\r\n");
            strRet.append(t.getType() + "[" + t.getBeginRowIndex() + ":" + t.getBeginColumn() + "]  ");
            strRet.append(t.getInitString().replaceAll("\\r","").replaceAll("\\n"," "));
        }
        return strRet.toString();
    }
}
